﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace Du_Lich
{
    public partial class QuanLyDatTourForm : Form
    {
        public QuanLyDatTourForm()
        {
            InitializeComponent();
        }

        public QuanLyDatTourForm(string makh)
        {
            InitializeComponent();
            txtMaKH.Text = makh;
        }

        string strConn = ConfigurationManager.ConnectionStrings["DuLich"].ConnectionString;
        private SqlConnection conn = null;
        private SqlCommand cmd = null;
        private SqlDataAdapter da = null;

        public void MoKetNoi()
        {
            conn = new SqlConnection(strConn);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
        }

        public void DongKetNoi()
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }
       
        private void QuanLyDatTourForm_Load(object sender, EventArgs e)
        {
            MoKetNoi();
            txtMaNV.Text = DangNhapForm.manv.ToString();
            gvThongTinDatTour.Columns.Clear();
            gvThongTinDatTour.DataSource = DanhSachDatTour();
          
            cbtentour.DataSource = LoadTenTour();
            cbtentour.DisplayMember = "TenTour";
            cbtentour.ValueMember = "MaTour";
        }

        private void QuanLyDatTourForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DongKetNoi();
        }

        public DataTable LoadTenTour()
        {
            string strTenTour = "select MaTour from TOUR";
            da = new SqlDataAdapter(strTenTour, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public DataTable DanhSachDatTour()
        {
            string strDanhSach = "select * from DATTOUR";
            SqlDataAdapter adapter = new SqlDataAdapter(strDanhSach, conn);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            return dt;
           
        }

       public void Them(string madt,string ngaydt,string ngaydi,string ngayve,int songuoi,string manv,string makh,string matour,string gc)
       {
           string strInsert = "Insert DATTOUR values(@madt,@ngaydt,@ngaydi,@ngayve,@songuoi,@manv,@makh,@matour,@ghichu)";
           cmd = new SqlCommand(strInsert, conn);
           cmd.Parameters.AddWithValue("@madt", madt);
           cmd.Parameters.AddWithValue("@ngaydt", ngaydt);
           cmd.Parameters.AddWithValue("@ngaydi", ngaydi);
           cmd.Parameters.AddWithValue("@ngayve", ngayve);
           cmd.Parameters.AddWithValue("@songuoi", songuoi);
           cmd.Parameters.AddWithValue("@manv", manv);
           cmd.Parameters.AddWithValue("@makh", makh);
           cmd.Parameters.AddWithValue("@matour", matour);
           cmd.Parameters.AddWithValue("@ghichu", gc);
           if (conn.State == ConnectionState.Closed)
               conn.Open();
           cmd.ExecuteNonQuery();
           conn.Close();
       }

        public void Sua(string ndt,string nd,string nv,int sn,string gc,string madt)
        {
            string strUpdate = "Update DATTOUR set NgayDatTour=@ndt,NgayDi=@nd,NgayVe=@nv,SoNguoi=@sn,GhiChu=@gc where MaDatTour=@mdt";
            cmd = new SqlCommand(strUpdate, conn);
            cmd.Parameters.AddWithValue("@ndt", ndt);
            cmd.Parameters.AddWithValue("@nd", nd);
            cmd.Parameters.AddWithValue("@nv", nv);
            cmd.Parameters.AddWithValue("@sn", sn);
            cmd.Parameters.AddWithValue("@gc", gc);
            cmd.Parameters.AddWithValue("mdt", madt);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            
        }

        public void Xoa(string madt)
        {
            string strDelete = "Delete DATTOUR where MaDatTour=@madt";
            cmd = new SqlCommand(strDelete, conn);
            cmd.Parameters.AddWithValue("@madt", madt);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            

        }

        private int TimKiem()
        {
            string chuoitim = txtMaDatTour.Text;
            int i = -1;
            foreach (DataGridViewRow dgv in gvThongTinDatTour.Rows)
            {
                try
                {
                    i++;
                    if (string.Compare(chuoitim, dgv.Cells[0].Value.ToString()) == 0)
                        return i;
                }
                catch (Exception)
                {
                    MessageBox.Show("Ban nhap sai roi ! Nhap lai di");
                }
            }
            return -1;
        }

       private void btnThem_Click(object sender, EventArgs e)
       {
           try
           {
               Them(txtMaDatTour.Text, datetimeDatTour.Value.ToString("MM/dd/yyyy"), DateTimeNgayDi.Value.ToString("MM/dd/yyyy"), DateTimeNgayVe.Value.ToString("MM/dd/yyyy"), int.Parse(txtSoNguoi.Text), txtMaNV.Text, txtMaKH.Text, cbtentour.Text,txtGhiChu.Text);
               gvThongTinDatTour.DataSource = DanhSachDatTour();
           }
           catch(Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

       private void btnXoa_Click(object sender, EventArgs e)
       {
           if (gvThongTinDatTour.SelectedRows.Count == 0)
               return;
           int i = gvThongTinDatTour.SelectedRows[0].Index;
           Xoa(gvThongTinDatTour.Rows[i].Cells[0].Value.ToString());
           gvThongTinDatTour.DataSource = DanhSachDatTour();
       }

       private void btnTim_Click(object sender, EventArgs e)
       {
           int kq = TimKiem();
           if (kq == -1)
               MessageBox.Show("Khong tim thay", "Thong bao tim kiem");
           else
               gvThongTinDatTour.Rows[kq].Selected = true;
       }

       private void button1_Click(object sender, EventArgs e)
       {
           DongKetNoi();
           this.Close();
       }

     

       private void btnSua_Click(object sender, EventArgs e)
       {
           Sua(datetimeDatTour.Value.ToString("MM/dd/yyyy"), DateTimeNgayDi.Value.ToString("MM/dd/yyyy"), DateTimeNgayVe.Value.ToString("MM/dd/yyyy"), int.Parse(txtSoNguoi.Text), txtGhiChu.Text, txtMaDatTour.Text);
           gvThongTinDatTour.DataSource = DanhSachDatTour();
       }

       private void gvThongTinDatTour_SelectionChanged(object sender, EventArgs e)
       {
           if (gvThongTinDatTour.SelectedRows.Count == 0)
               return;
           int i = gvThongTinDatTour.SelectedRows[0].Index;
           txtMaDatTour.Text = gvThongTinDatTour.Rows[i].Cells[0].Value.ToString();
           datetimeDatTour.Value = DateTime.Parse(gvThongTinDatTour.Rows[i].Cells[1].Value.ToString());
           DateTimeNgayDi.Value = DateTime.Parse(gvThongTinDatTour.Rows[i].Cells[2].Value.ToString());
           DateTimeNgayVe.Value = DateTime.Parse(gvThongTinDatTour.Rows[i].Cells[3].Value.ToString());
           txtSoNguoi.Text = gvThongTinDatTour.Rows[i].Cells[4].Value.ToString();
           txtMaKH.Text = gvThongTinDatTour.Rows[i].Cells[5].Value.ToString();
           txtGhiChu.Text = gvThongTinDatTour.Rows[i].Cells[8].Value.ToString();
           cbtentour.Text = gvThongTinDatTour.Rows[i].Cells[6].Value.ToString();
       }
    }
}
